﻿using System.Threading.Tasks;
using Dpx.Models;
using Dpx.Views;
using GalaSoft.MvvmLight.Messaging;
using Xamarin.Forms;

namespace Dpx.Services {
    /// <summary>
    /// 根导航服务。
    /// </summary>
    public class RootNavigationService : IRootNavigationService {
        // ******** 构造函数

        /// <summary>
        /// 根页面激活服务。
        /// </summary>
        private readonly IRootPageActivationService _rootPageActivationService;

        /// <summary>
        /// 根导航服务。
        /// </summary>
        /// <param name="rootPageActivationService">根页面激活服务。</param>
        public RootNavigationService(
            IRootPageActivationService rootPageActivationService) {
            _rootPageActivationService = rootPageActivationService;
        }

        /// <summary>
        /// 导航工具。
        /// </summary>
        private MainPage MainPage =>
            _mainPage ??= Application.Current.MainPage as MainPage;

        private MainPage _mainPage;

        /******** 继承方法 ********/

        /// <summary>
        /// 导航到页面。
        /// </summary>
        /// <param name="pageKey">页面键。</param>
        public async Task NavigateToAsync(string pageKey) {
            await MainPage.Detail.Navigation.PopToRootAsync();
            var page = _rootPageActivationService.Activate(pageKey);
            await page.Navigation.PopToRootAsync();
            MainPage.Detail = page;
            if (MainPage.MasterBehavior == MasterBehavior.Popover) {
                if (Device.RuntimePlatform == Device.Android) {
                    await Task.Delay(100);
                }

                MainPage.IsPresented = false;
            }
            Messenger.Default.Send(
                new RootNavigationMessage { PageKey = pageKey });

        }

        /// <summary>
        /// 导航到页面。
        /// </summary>
        /// <param name="pageKey">页面键。</param>
        /// <param name="parameter">参数。</param>
        public async Task NavigateToAsync(string pageKey, object parameter) {
            var page = _rootPageActivationService.Activate(pageKey);
            await page.Navigation.PopToRootAsync();
            NavigationContext.SetParameter(page.CurrentPage, parameter);
            MainPage.Detail = page;
            if (MainPage.MasterBehavior == MasterBehavior.Popover) {
                if (Device.RuntimePlatform == Device.Android) {
                    await Task.Delay(100);
                }

                MainPage.IsPresented = false;
            }
            Messenger.Default.Send(
                new RootNavigationMessage { PageKey = pageKey });

        }
    }

    /// <summary>
    /// 根导航常量。
    /// </summary>
    public static class RootNavigationConstant {
        /// <summary>
        /// 诗词搜索页。
        /// </summary>
        public static readonly string QueryPage = nameof(Views.QueryPage);

        /// <summary>
        /// 今日推荐页。
        /// </summary>
        public static string TodayPage = nameof(Views.TodayPage);

        /// <summary>
        /// 初始化页。
        /// </summary>
        public static readonly string InitializationPage =
            nameof(Views.InitializationPage);

        /// <summary>
        /// 诗词收藏页。
        /// </summary>
        public static readonly string FavoritePage = nameof(Views.FavoritePage);

        /// <summary>
        /// 数据同步页。
        /// </summary>
        public static readonly string SyncPage = nameof(Views.SyncPage);

    }
}